﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace DrivingAlarm.Website
{
    class HZMotorB : WebsiteBase
    {
        private const string WEB_URL = @"http://xqc.qc5qc.com/training/show/00{0}";
        private static string[] PLACE_NAME = new string[] 
        { "城北汽车站", "保捷公司点", "铁路培训点", "浙江经职培训点","交通职高"};
        private static int[] DriverPlaceIdx = new int[] { 1, 2, 4,  6, 8, 11 };
        public HZMotorB()
        {
            this.WebsiteName = "杭州市机动车驾驶员";
        }
        public override void WatchOne()
        {
            for (int idx = 0; idx < DriverPlaceIdx.Length; idx++)
            {
                string strPage = GetPageStr(string.Format(WEB_URL, DriverPlaceIdx[idx]));
                if (!AnalysisStr(strPage, idx))
                {
                    MSG_STRUCT msg = new MSG_STRUCT();
                    msg.MsgStr = string.Format("{0}\t不可以预约", PLACE_NAME[idx]);
                    msg.bPlaySound = false;
                    SendMessage(Hwnd_Parent, (int)WM_MSG.WM_MSG_UPDATE_INFO, IntPtr.Zero, ref msg);
                }
            }
        }
        private bool AnalysisStr(string str, int placeIdx)
        {
            string pattern = @"(?<=\>)(\d{1,}(?=人)|\d{4}-\d{2}-\d{2}(?=\<))";
            MatchCollection mc = Regex.Matches(str, pattern, RegexOptions.IgnoreCase);
            int idxBase;
            bool bRet = false;
            if (mc.Count > 0)
            {
                for (int idx = 0; idx < mc.Count / 5; idx++)
                {
                    idxBase = idx * 5;
                    DateTime date = Convert.ToDateTime(mc[idxBase].Value);
                    int totalDay = (int)(date - DateTime.Today).TotalDays;
                    if (totalDay > WatchDay)
                    {
                        continue;
                    }
                    for (int i = 1; i < 3; i++)
                    {
                        if (Convert.ToInt32(mc[idxBase + i].Value) > Convert.ToInt32(mc[idxBase +i+ 1].Value))
                        {
                            if (Convert.ToInt32(mc[idxBase + i + 1].Value) == 0)
                            {
                                continue;
                            }
                            MSG_STRUCT msg = new MSG_STRUCT();
                            msg.MsgStr = string.Format("{0}\t阶段{1}\t可预约\t{2}",
                                PLACE_NAME[placeIdx],i ,mc[idxBase]);
                            msg.bPlaySound = true;
                            SendMessage(Hwnd_Parent, (int)WM_MSG.WM_MSG_UPDATE_INFO, IntPtr.Zero, ref msg);
                            bRet = true;
                        }
                    }
                   
                }
            }
            return bRet;
        }
    }
}
